package com.atguigu.gmall.seckill.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.seckill.biz.SeckillBizService;
import com.atguigu.gmall.seckill.entity.SeckillGoods;
import com.atguigu.gmall.seckill.service.SeckillGoodsService;
import com.atguigu.gmall.seckill.vo.SeckillOrderConfirmVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "秒杀Rpc")
@RestController
@RequestMapping("/api/inner/rpc/seckill")
public class SeckillRpcController {

    @Autowired
    SeckillGoodsService seckillGoodsService;
    @Autowired
    SeckillBizService seckillBizService;

    @ApiOperation("查询当天参与秒杀的所有商品")
    @GetMapping("/today/goods")
    public Result<List<SeckillGoods>> getTodaySeckillGoods () {
        ///2022-01-10 格式化时间 获取今天的秒杀商品
        String date = DateUtil.formatDate(new Date());
        //调用方法查询
        //List<SeckillGoods> goods = seckillGoodsService.getSeckillGoodsByDay(date);//从数据库中查询商品
        List<SeckillGoods> goods = seckillGoodsService.getSeckillGoodsByDayFromCache(date);//从缓存中查询数据商品
        return Result.ok(goods);
    }

    @ApiOperation("查询秒杀商品详情")
    @GetMapping("/detail/{skuId}")
    public Result<SeckillGoods> getSeckillGoodsDetail(@PathVariable("skuId") Long skuId) {
        SeckillGoods seckillGoods = seckillGoodsService.getDetail(skuId);
        return Result.ok(seckillGoods);
    }

    @ApiOperation("获取秒杀单数据")
    @GetMapping("/order/detail/{code}")
    public Result<SeckillOrderConfirmVo> getSeckillOrderInfo(@PathVariable("code") String code){
        SeckillOrderConfirmVo confirmVo = seckillBizService.getSeckillOrderInfo(code);
        return Result.ok(confirmVo);
    }
}
